package com.iot.manage.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.SysParam;
import com.iot.manage.result.Code;
import com.iot.manage.result.Result;
import com.iot.manage.service.SysParamsService;
import com.iot.manage.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统参数管理控制层
 *
 * @author zhuzhi
 */
@RestController
@RequestMapping("/api/private/v1")
public class SysParamsController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysParamsService sysParamsService;

    /**
     * 查询系统参数信息列表
     *
     * @param token
     * @param query 模糊查询关键字
     * @param pagenum 当前页
     * @param pagesize 每页条数
     * @return
     */
    @GetMapping("/findSysParamsList")
    public Result findSysParamsList(
            @RequestHeader("token") String token,
            @RequestParam("query") String query,
            @RequestParam("pagenum") int pagenum,
            @RequestParam("pagesize") int pagesize) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        List<SysParam> sysParamsList;
        int sysParamsTotal;
        try{
            sysParamsList = sysParamsService.findSysParamsList(query,pagenum,pagesize);
            sysParamsTotal = sysParamsService.countSysParamsList(query);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",sysParamsTotal);
        map.put("pagenum",pagenum);
        map.put("sysParamsList",sysParamsList);
        return result.build(map, Code.S200);
    }

    /**
     * 新增系统参数信息
     *
     * @param token
     * @param params 系统参数信息
     * @return
     */
    @PostMapping("/addSysParamsInfo")
    public Result addSysParamsInfo(
            @RequestHeader("token") String token,
            @RequestBody JSONObject params) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            if(null != findSysParamsInfoByParamEnName(token,params.getString("paramEnName")).getData()){
                return result.build(null, Code.D412);
            }
            sysParamsService.addSysParamsInfo(params);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     * 根据系统参数英文名查询系统参数信息
     *
     * @param token
     * @param paramEnName 系统参数英文名
     * @return
     */
    @GetMapping("/findSysParamsInfoByParamEnName/{paramEnName}")
    public Result findSysParamsInfoByParamEnName(
            @RequestHeader("token") String token,
            @PathVariable("paramEnName") String paramEnName){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        SysParam sysParam;
        try{
            sysParam = sysParamsService.findSysParamsInfoByParamEnName(paramEnName);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(sysParam, Code.S200);
    }

    /**
     * 修改系统参数信息
     *
     * @param token
     * @param params 系统参数信息参数
     * @return
     */
    @PutMapping("/updateSysParamsInfo/{oldSysParamsEnName}")
    public Result updateSysParamsInfo(
            @RequestHeader("token") String token,
            @PathVariable("oldSysParamsEnName") String oldSysParamsEnName,
            @RequestBody JSONObject params){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            if (!(oldSysParamsEnName.equals(params.get("paramEnName")))){
                if(null != findSysParamsInfoByParamEnName(token,params.getString("paramEnName")).getData()){
                    return result.build(null, Code.D412);
                }
            }
            params.put("oldSysParamsEnName",oldSysParamsEnName);
            sysParamsService.updateSysParamsInfo(params);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     *删除系统参数信息
     *
     * @param token
     * @param paramsEnName 系统参数英文名
     * @return
     */
    @DeleteMapping("/removeSysParamsInfo/{paramsEnName}")
    public Result removeSysParamsInfo(
            @RequestHeader("token") String token,
            @PathVariable("paramsEnName") String paramsEnName) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            sysParamsService.removeSysParamsInfo(paramsEnName);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }
}
